/**
 * Provides a managed resizable panel facility.
 * @namespace GW.panel
 * @static
 * @requires 
 * @module gwpanels
 * @author RBell 2009
 */

GW.panel = {};
var GWP = GW.panel;

// Default panel name lead string
GWP.SETNAME = 'gwp-';
// Panel set index to serializes panel names, ex. gwp-0, gwp-1, ... .
GWP._panelIndex = 0;
/** customize panelConfig
*/
GWP.config = function( config ){
    var nConfig = {};
    for ( i in GW.layout.panelConfig ){
        nConfig[i] = GW.layout.panelConfig[i];
    }
    for (i in config ){
        nConfig[i] = config[i];
    }
    return nConfig;
};
/** Create a new panel in the panel set.
 * @static
 * @method NewPanel 
 * @param {String} name The id of the panel.  If '' an unique id will be created.
 * @param {object} config Configuration parameters for this panel.  See YAHOO.widget.Panel.
 * @return {Object} The panel Object
 */
GWP.newPanel = function( name, config ){
    // if there is no name or it is '' create one
    if( !name || name == ''){
        name = GWP.SETNAME + GWP._panelIndex++;
    }
    // create the panel
    var tmpPanel = new YAHOO.widget.Panel( name, GW.layout.panelConfig );
    // merge the config with the default config
    if( config ){
        tmpPanel.cfg.applyConfig( config, true );
    }
    // create the overlayManager if it doesn't already exist
    if( typeof GWP.overlayManager == 'undefined' ){ GWP.overlayManager = new YAHOO.widget.OverlayManager(); }
    // register with overlay manager
    GWP.overlayManager.register([tmpPanel]);
    // force content if there is none
    if( !Boolean( tmpPanel.header )) {
        tmpPanel.setHeader( tmpPanel.id + " header " );
    }
    if( !Boolean( tmpPanel.body )) {
        tmpPanel.setBody( tmpPanel.id + " body" );
    }
    //if( !Boolean( tmpPanel.footer )) {
        //tmpPanel.setFooter( tmpPanel.id );
    //};
    // render the panel
    tmpPanel.render( document.body );
    // add tooltip
    //tmpPanel._tt_ = new YAHOO.widget.Tooltip(name+'_tt', { 
        //context: name+'_h', 
        //zindex: 999,
        //text:"Dragable & resizable panel" 
    //});
    // setup hide/show
    tmpPanel._show = true;
    return tmpPanel;
};

/** Make panel resizable.
 * @static
 * @method Resize 
 * @param {object} panel The panel to make resizable.
 * @return {Object} The panel Object
 */
GWP.resize = function( panel ){
    // Hold the resizeConfig in a local var since Resize dies otherwise!
    // Not sure why but it seems likely that YUI resize stores instance 
    // values in the config and this causes problems.
    var resizeConfig = {
        handles: ['br', 'tr'],
        autoRatio: false,
        minWidth: 200,
        minHeight: 100,
        status: false,
        proxy: true
    };
    var resize = new YAHOO.util.Resize( panel.id, resizeConfig );
    panel._resize_ = resize;
    // Setup startResize handler, to constrain the resize width/height
    // if the constraintoviewport configuration property is enabled.
    resize.on("startResize", function(args) {
        if (panel.cfg.getProperty("constraintoviewport")) {
            var D = YAHOO.util.Dom;
            var clientRegion = D.getClientRegion();
            var elRegion = D.getRegion(this.element);
            resize.set("maxWidth", 
                clientRegion.right - elRegion.left - 
                YAHOO.widget.Overlay.VIEWPORT_OFFSET);
            resize.set("maxHeight", 
                clientRegion.bottom - elRegion.top - 
                YAHOO.widget.Overlay.VIEWPORT_OFFSET);
        } else {
            resize.set("maxWidth", null);
            resize.set("maxHeight", null);
        }
    }, panel, true);
    // Setup resize handler to update the Panel's 'height' to cause body to 
    // fill the new height.
    resize.on("resize", function(args) {
        var panelHeight = args.height;
        var panelWidth = args.width;
        if (panelHeight != 0) {
            this.cfg.setProperty("height", panelHeight + "px");
            }
        if (panelWidth != 0 ) {
            this.cfg.setProperty("width", panelWidth + "px");
            }
        // Check if this is a map panel and resize map if so. 
        // Panels may or may not have maps.  If they do, the map is stored in
        // _map so that it can be resized when the panel is resized.  This is
        // a bit of a hack but effective and it allows multiple panels to have
        // maps at the same time.
        if( panel._map ){       // this is a map panel
            panel._map.checkResize();
        }
    }, panel, true);
    return panel;
};

/** Show/hide the panel
 * @static
 * @method Show 
 */
GWP.show = function( args, panel ){
    if( panel._show ){
        panel.hide();
        panel._show = false;
    } else {
        panel.show();
        panel._show = true;
    }
};

GW.panelsLoaded = true;

